﻿using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebServerConnectorDll;

namespace FridgeManagerApp
{
    class PersonnelInsertViewModel : NotificationObject
    {
        PersonnelInsert gWindow;
        PersonnelViewModel gParentViewModel;

        public PersonnelInsertViewModel(PersonnelInsert pWindow, PersonnelViewModel pParentViewModel)
        {
            gWindow = pWindow;
            gParentViewModel = pParentViewModel;

            List<WPersonnelGroup> list = WPersonnelGroup.GetAll();
            foreach(var item in list)
            {
                PersonnelGroups.Add(item.PersonnelGroupName.Trim());
            }
        }

        private String id;
        public String ID
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
                RaisePropertyChanged("ID");
            }
        }

        private String password;
        public String Password
        {
            get
            {
                return password;
            }
            set
            {
                password = value;
                RaisePropertyChanged("Password");
            }
        }

        private List<String> personnelGroups = new List<String>();
        public List<String> PersonnelGroups
        {
            get
            {
                return personnelGroups;
            }

            set
            {
                personnelGroups = value;
                RaisePropertyChanged("PersonnelGroups");
            }
        }

        private String personnelGroupsSelected;
        public String PersonnelGroupsSelected
        {
            get
            {
                return personnelGroupsSelected;
            }

            set
            {
                personnelGroupsSelected = value;
                RaisePropertyChanged("PersonnelGroupsSelected");
            }
        }

        private String name;
        public String Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
                RaisePropertyChanged("Name");
            }
        }

        private String head;
        public String Head
        {
            get
            {
                return head;
            }
            set
            {
                head = value;
                RaisePropertyChanged("Head");
            }
        }

        private String mobile;
        public String Mobile
        {
            get
            {
                return mobile;
            }
            set
            {
                mobile = value;
                RaisePropertyChanged("Mobile");
            }
        }

        private String webChat;
        public String WebChat
        {
            get
            {
                return webChat;
            }
            set
            {
                webChat = value;
                RaisePropertyChanged("WebChat");
            }
        }

        protected DelegateCommand<object> insertCommand;
        public DelegateCommand<object> InsertCommand
        {
            get
            {
                return insertCommand ?? (insertCommand = new DelegateCommand<object>(AInsertCommand, BInsertCommand));
            }
        }

        protected virtual void AInsertCommand(object obj)
        {
            List<WPersonnel> list = new List<WPersonnel>();
            WPersonnel personnel = new WPersonnel();
            personnel.ID = ID;
            personnel.Password = Password;
            personnel.PersonnelGroupUID = WPersonnelGroup.GetPersonnelGroupUID(PersonnelGroupsSelected);
            personnel.Name = Name;
            personnel.Head = Head;
            personnel.Mobile = Mobile;
            personnel.WebChat = WebChat;
            list.Add(personnel);
            HttpResponse response = WPersonnel.Insert(list);

            gWindow.Close();
            gParentViewModel.UpdateList();
        }

        private bool BInsertCommand(object arg)
        {
            return true;
        }

        protected DelegateCommand<object> quitCommand;
        public DelegateCommand<object> QuitCommand
        {
            get
            {
                return quitCommand ?? (quitCommand = new DelegateCommand<object>(AQuitCommand, BQuitCommand));
            }
        }

        protected virtual void AQuitCommand(object obj)
        {
            gWindow.Close();
            gParentViewModel.UpdateList();
        }

        private bool BQuitCommand(object arg)
        {
            return true;
        }
    }
}
